package com.czh.circle.api.common;

import lombok.Data;

@Data
public class ResponseResult<T> {

    private Boolean success;

    private Integer code;

    private String message;

    private T data;

    ResponseResult(){
        this.success = true;
        this.code = ResultCodeEnum.SUCCESS.getCode();
        this.message = ResultCodeEnum.SUCCESS.getDesc();
    }

    public static ResponseResult<Boolean> ok(){
        return new ResponseResult<>();
    }

    public static <T> ResponseResult<T> ok(T data){
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setData(data);
        return responseResult;
    }

    public static ResponseResult<Boolean> fail(){
        ResponseResult<Boolean> responseResult = new ResponseResult<>();
        responseResult.setError(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getDesc(), null);
        return responseResult;
    }

    public static <T> ResponseResult<T> fail(String message){
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setError(ResultCodeEnum.FAIL.getCode(), message, null);
        return responseResult;
    }
    public static <T> ResponseResult<T> fail(T data){
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setError(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getDesc(), data);
        return responseResult;
    }

    private void setError(Integer code, String message, T data) {
        this.success = false;
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
